/**
 * 初始化银行卡表详情对话框
 */
var UsersBankcardsInfoDlg = {
    usersBankcardsInfoData : {}
};

/**
 * 清除数据
 */
UsersBankcardsInfoDlg.clearData = function() {
    this.usersBankcardsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
UsersBankcardsInfoDlg.set = function(key, val) {
    this.usersBankcardsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
UsersBankcardsInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
UsersBankcardsInfoDlg.close = function() {
    parent.layer.close(window.parent.UsersBankcards.layerIndex);
}

/**
 * 收集数据
 */
UsersBankcardsInfoDlg.collectData = function() {
    this
    .set('bankCardId')
    .set('userId')
    .set('type')
    .set('provinceId')
    .set('cityId')
    .set('areaId')
    .set('bankId')
    .set('accArea')
    .set('accNo')
    .set('phone')
    .set('dataFlag')
    .set('createTime');
}

/**
 * 提交添加
 */
UsersBankcardsInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/usersBankcards/add", function(data){
        Feng.success("添加成功!");
        window.parent.UsersBankcards.table.refresh();
        UsersBankcardsInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.usersBankcardsInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
var showInfo = ["#type",'#accArea','#accNo','#phone'];
UsersBankcardsInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    var flag = 0;
    showInfo.forEach(function (value,index,array) {
        if(!$(value).val()){
            if(!$.isEmptyObject($(value).next())){
                flag ++;
                $(value).parent().append("<span style='color: red'>"+$(value).parent().prev().html()+"不能为空</span>");

            }
        }else{
            $(value).next().remove();
        }
    });

    if(flag==0){
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/usersBankcards/update", function(data){
            Feng.success("修改成功!");
            window.parent.UsersBankcards.table.refresh();
            UsersBankcardsInfoDlg.close();
        },function(data){
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.usersBankcardsInfoData);
        ajax.start();
    }
}

$(function() {
    if($("#type2").is(":empty")){
        var type = $('#type2').val();
        switch (type){
            case '1':
                $('#type').val('默认');
                break;
            case '2':
                $('#type').val('不是');
                break;
            default:
                break;
        }
    }
});
